package pack2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author wenze-zero
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(10000);
        System.out.println("等待链接");
        Socket s=ss.accept();
        System.out.println("连接成功");
        ServerThread ST=new ServerThread(s);
        Thread t1=new Thread(ST);
        InputStream is=s.getInputStream();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String aline;
        byte[] bys=new byte[1024];
        int len;
        t1.start();
        while (true){
            len=is.read(bys);
            aline=new String(bys,0,len);
            System.out.println("客户端"+aline);
        }
    }
}
class ServerThread implements Runnable{
    private Socket s;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    public ServerThread(){
    }
    public ServerThread(Socket s){  this.s=s;  }
    @Override
    public void run() {
        try {
            while (true){
                String sline;
                sline=br.readLine();
                OutputStream os=s.getOutputStream();
                os.write(sline.getBytes());
                System.out.println("服务器端"+sline);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
